﻿@using PagedList
@using PagedList.Mvc
@using Great_Eggspectations.DAL
@using Great_Eggspectations.ViewModels
@model TransactionsIndexTableViewModel

<h3>Summary@(
    (!string.IsNullOrEmpty(ViewBag.PartnerName) ? " - " + ViewBag.PartnerName : "") +
    (!string.IsNullOrEmpty(ViewBag.ProductName) ? " - " + ViewBag.ProductName : "")
)</h3>
<p>
    Credits: @(Model.Credits.ToString("C")) &emsp;
    @if (string.IsNullOrEmpty(ViewBag.ProductName)) {
        <text>
            Partner Credits: @(Model.PartnerCredits.ToString("C")) &emsp;
            Debits: @(Model.Debits.ToString("C")) &emsp;
            Net Profits: @(Model.NetProfits.ToString("C"))
        </text>
    }
</p>

<table class="table">
    <tr>
        <th>Summary</th>
        <th>Date</th>
        <th>Partner</th>
        <th>Product</th>
        <th>Quantity</th>
        <th>Credit</th>
        <th>Debit</th>
        <th></th>
    </tr>

    @foreach (Transaction item in Model.TransactionsPagedList)
    {
        <tr>
            <td>
                @item.Summary
            </td>
            <td>
                @item.Date.ToShortDateString()
            </td>
            <td>
                @Html.ActionLink(item.Partner.Name, "Details", "Partner", new { id = item.Partner.Id }, new { })
            </td>
            <td>
                @Html.ActionLink(item.Product.Name, "Details", "Product", new { id = item.Product.Id }, new { })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Quantity)
            </td>
            <td>
                @if(item.IsSale) {
                    @item.Payment.ToString("C")
                }
            </td>
            <td>
                @if (!item.IsSale)
                {
                    @item.Payment.ToString("C")
                }
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", "Transaction", new { id = item.Id }, new { } ) |
                @Html.ActionLink("Details", "Details", "Transaction", new { id = item.Id }, new { }) |
                @Html.ActionLink("Delete", "Delete", "Transaction", new { id = item.Id }, new { })
            </td>
        </tr>
    }

</table>

<div class="col-sm-12">
    <div class="center">
        @Html.PagedListPager(Model.TransactionsPagedList, page => Url.Action("Index", new {
            partnerId = ViewBag.PartnerId,
            productId = ViewBag.ProductId, 
            page, 
            pageSize = Model.TransactionsPagedList.PageSize 
        }))
        Showing @Model.TransactionsPagedList.FirstItemOnPage 
        to @Model.TransactionsPagedList.LastItemOnPage 
        of @Model.TransactionsPagedList.TotalItemCount Transactions
    </div>
</div>
<br />
